﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using blackenter.Business.Exceptions;
using blackenter.Business.Sites;
using blackenter.Data;

namespace blackenter.Business.Pages {
    public class CmsPage : IPage {

        #region Fields

        private readonly IPageDbContext dbContext;
        private int siteId;
        private CultureInfo siteCulture = CultureInfo.InvariantCulture;

        #endregion

        #region Properties

        public int PageId { get; private set; }

        public int SiteId {
            get {
                return this.siteId;
            }
            set {
                if (this.siteId > 0)
                    throw new PageException("Move page to other site is not allowed!");
                this.siteId = value;
            }
        }
        public string PageName { get; set; }
        public CultureInfo SiteCulture {
            get { return this.siteCulture; }
            set {
                if (this.PageId > 0 && !this.siteCulture.Equals(CultureInfo.InvariantCulture))
                    throw new PageException("Move page to other culture is not allowed");
                this.siteCulture = value;
            }
        }

        #endregion

        #region Constructors

        public CmsPage() {
            this.dbContext = DbFactory.GetPageDbContext();
        }

        public CmsPage(int pageId)
            : this() {
            this.ReadPage(pageId);
        }

        internal CmsPage(PageData data)
            : this() {
            this.ReadPage(data);
        }

        #endregion

        #region Methods

        private void ReadPage(int pageId) {
            PageData data = this.dbContext.GetPageByPageId(pageId);
            this.ReadPage(data);
        }

        private void ReadPage(PageData data) {
            if (data.Id == 0) return;
            this.PageId = data.Id;
            this.SiteId = data.SiteId;
            this.PageName = data.Name;
            this.SiteCulture =
                String.IsNullOrEmpty(data.SiteCulture) ?
                    CultureInfo.InvariantCulture :
                    CultureInfo.GetCultureInfo(data.SiteCulture);
        }


        public bool Save() {
            if (this.SiteId == 0) throw new PageException("You trying add page to non existed site");
            var site = new CmsSite(this.SiteId);
            if (!site.Exists) throw new PageException("You trying add page to non existed site");

            if (this.SiteCulture == null || this.SiteCulture.Equals(CultureInfo.InvariantCulture)) {
                var languageSite = new CmsSiteLanguage(this.SiteId);
                this.SiteCulture = languageSite.DefaultLanguage;
            }

            if (this.PageId > 0) {
                return this.dbContext.SavePage(this.PageId, this.PageName, this.SiteCulture.Name);
            } else {

                int pageId = this.dbContext.SaveNewPage(this.SiteId, this.PageName, this.SiteCulture.Name);
                if (pageId <= 0) return false;
                this.PageId = pageId;
                return true;
            }
        }

        #endregion

    }
}
